
function paramsFormat(params) {
    let keys = Object.keys(params);
    let str = ''
    keys.forEach(function (item, index) {
        str = str + `${item}=${params[item]}&`
        // console.log(str);
    })
    str = str.slice(0, -1);
    return str;
}

const http = {
    get: function (url, data = { params: {} }, callback) {
        let res = null;
        const params = data.params;
        let str = paramsFormat(params);

        let xhr = new XMLHttpRequest();
        xhr.open('get', url + '?' + str);
        xhr.send();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                res = JSON.parse(xhr.responseText)
                callback(res)
            }
        }
    },



    post: function (url, data, callback) {
        let res = null;

        let xhr = new XMLHttpRequest();
        xhr.open('post', url);
        xhr.setRequestHeader('content-type', 'application/JSON');
        xhr.send(JSON.stringify(data))
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                res = JSON.parse(xhr.responseText)
                callback(res)
            }
        }
    },



    upload: function (url, data, callback) {
        let res = null;

        let xhr = new XMLHttpRequest();
        xhr.open('post', url);
        xhr.send(data)
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                res = JSON.parse(xhr.responseText)
                callback(res)
            }
        }
    }


}